package com.cmc.cloud.cmclink.doc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.GuaranteeLetterErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.vo.guaranteelettervo.GuaranteeLetterCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.guaranteelettervo.GuaranteeLetterPageReqVO;
import com.cmc.cloud.cmclink.doc.vo.guaranteelettervo.GuaranteeLetterUpdateReqVO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;


import java.util.*;

import com.cmc.cloud.cmclink.doc.service.GuaranteeLetterService;
import com.cmc.cloud.cmclink.doc.entity.GuaranteeLetterDO;
import com.cmc.cloud.cmclink.doc.mapper.GuaranteeLetterMapper;


/**
 * 长期保函 Service 实现类
 *
 * @author 招商轮船-管理员
 */
@Service
@Validated
public class GuaranteeLetterServiceImpl implements GuaranteeLetterService {

    @Resource
    private GuaranteeLetterMapper guaranteeLetterMapper;

    @Override
    public void createGuaranteeLetter(GuaranteeLetterCreateReqVO createReqVO) {
        // 插入
        GuaranteeLetterDO guaranteeLetter = BeanUtil.copyProperties(createReqVO, GuaranteeLetterDO.class);
        guaranteeLetterMapper.insert(guaranteeLetter);
    }

    @Override
    public void updateGuaranteeLetter(GuaranteeLetterUpdateReqVO updateReqVO) {
        // 校验存在
        validateGuaranteeLetterExists(updateReqVO.getId());
        // 更新
        GuaranteeLetterDO updateObj = BeanUtil.copyProperties(updateReqVO, GuaranteeLetterDO.class);
        guaranteeLetterMapper.updateById(updateObj);
    }

    @Override
    public void deleteGuaranteeLetter(Long id) {
        // 校验存在
        validateGuaranteeLetterExists(id);
        // 删除
        guaranteeLetterMapper.deleteById(id);
    }

    private void validateGuaranteeLetterExists(Long id) {
        if (guaranteeLetterMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(GuaranteeLetterErrorCodeConstants.GUARANTEE_LETTER_NOT_EXISTS);
        }
    }

    @Override
    public GuaranteeLetterDO getGuaranteeLetter(Long id) {
        return guaranteeLetterMapper.selectById(id);
    }

    @Override
    public List<GuaranteeLetterDO> getGuaranteeLetterList(Collection<Long> ids) {
        return guaranteeLetterMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<GuaranteeLetterDO> getGuaranteeLetterPage(GuaranteeLetterPageReqVO reqVO) {
        LambdaQueryWrapperX<GuaranteeLetterDO> queryWrapperX = new LambdaQueryWrapperX<GuaranteeLetterDO>()
                .eqIfPresent(GuaranteeLetterDO::getAttatchTypeCode, reqVO.getAttatchTypeCode());
        if (StringUtils.isNotEmpty(reqVO.getScnCode())) {
            queryWrapperX.and(wrapper -> wrapper.eq(GuaranteeLetterDO::getBkpCode, reqVO.getScnCode()).or()
                    .eq(GuaranteeLetterDO::getConsigneeCode, reqVO.getScnCode()).or()
                    .eq(GuaranteeLetterDO::getNotifyPartyCode, reqVO.getScnCode()).or()
                    .eq(GuaranteeLetterDO::getShipperCode, reqVO.getScnCode()).or()
                    .eq(GuaranteeLetterDO::getActualShipperCode, reqVO.getScnCode()));
        }
        if (StringUtils.isNotEmpty(reqVO.getPortCode())) {
            queryWrapperX.and(wrapper -> wrapper.like(GuaranteeLetterDO::getPodCodes, reqVO.getPortCode()).or()
                    .like(GuaranteeLetterDO::getPolCodes, reqVO.getPortCode()).or()
                    .like(GuaranteeLetterDO::getPorCodes, reqVO.getPortCode()).or()
                    .like(GuaranteeLetterDO::getDelCodes, reqVO.getPortCode()));
        }
        if (ObjectUtils.isNotEmpty(reqVO.getBeginDateStart())) {
            queryWrapperX.ge(GuaranteeLetterDO::getBeginDate, reqVO.getBeginDateStart());
        }
        if (ObjectUtils.isNotEmpty(reqVO.getBeginDateEnd())) {
            queryWrapperX.le(GuaranteeLetterDO::getBeginDate, reqVO.getBeginDateEnd());
        }
        return guaranteeLetterMapper.selectPage(reqVO, queryWrapperX);
    }

    @Override
    public List<GuaranteeLetterDO> queryGuaranteeLetterWeb(List<String> bkpCodes, Date queryTime) {
        LambdaQueryWrapper<GuaranteeLetterDO> queryWrapperX = new LambdaQueryWrapper<GuaranteeLetterDO>()
                .in(GuaranteeLetterDO::getBkpCode, bkpCodes)
                .le(Objects.nonNull(queryTime), GuaranteeLetterDO::getBeginDate, queryTime)
                .ge(Objects.nonNull(queryTime), GuaranteeLetterDO::getEndDate, queryTime);
        return guaranteeLetterMapper.selectList(queryWrapperX);
    }
}
